package mainpackage;

/*
 * Class Name: Weyr
 */
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Weyr {
	private final int SIZE = 1500;
	private Rider[] riders;
	private int count;

	public Weyr() {
		riders = new Rider[SIZE];
		count = 0;
	}

	public void addRider(Rider newRider) {

		// System.out.println("riders count = "+count);
		riders[count] = newRider;
		count++;
		System.out.println("Add a rider successfully and now there are "
				+ count + " riders!");
	}

	public void displayRiderDetail() {
		for (int i = 0; i < count; i++) {
			System.out.println(riders[i].toString());
		}
	}

	public void loadFromFile(String filename) throws FileNotFoundException {
		java.io.File file = new java.io.File(filename);
		if (!file.exists()) {
			System.out.println("riders information file " + filename
					+ " does not exist");
			System.exit(0);
		}
		java.util.Scanner input = new java.util.Scanner(file);
		while (input.hasNext()) {
			String name = input.nextLine();
			String code = input.nextLine();
			String flagStr = input.nextLine();
			boolean flag;
			if (flagStr.equals("true")) {
				flag = true;
			} else {
				flag = false;
			}
			String missions = input.nextLine();
			String missionType = input.nextLine();

			int mission1, mission2, mission3;
			String type1, type2, type3;
			boolean flag1 = false, flag2 = false, flag3 = false;
			mission3 = Integer.parseInt(missions.substring(missions
					.lastIndexOf(" ") + 1));
			mission2 = Integer.parseInt(missions.substring(
					missions.indexOf(" ") + 1, missions.lastIndexOf(" ")));
			mission1 = Integer.parseInt(missions.substring(0,
					missions.indexOf(" ")));

			type3 = missionType.substring(missionType.lastIndexOf(" ") + 1);
			type2 = missionType.substring(missionType.indexOf(" ") + 1,
					missionType.lastIndexOf(" "));
			type1 = missionType.substring(0, missionType.indexOf(" "));

			type1.trim();
			type2.trim();
			type3.trim();

			if (type1.equals("true")) {
				flag1 = true;
			} else {
				flag1 = false;
			}

			if (type2.equals("true")) {
				flag2 = true;
			} else {
				flag2 = false;
			}

			if (type3.equals("true")) {
				flag3 = true;
			} else {
				flag3 = false;
			}

			Rider newrider = new Rider(name, code, flag, mission1, mission2,
					mission3, flag1, flag2, flag3);
			addRider(newrider);
		}

	}// loadFromFile

	/*
	 * name:Rider'name type:value of 0/1/2,corresponding to
	 * training,transport,fall function:update that Rider's mission status
	 */
	public void mission(String name, int type) {
		int index = search(name);
		if (index == -1) {
			System.out.println("The name of a rider does not exist!");
			return;
		}
		System.out.println("the index = " + index);
		if (riders[index].availForMission() == true) {
			riders[index].setMission(type);
		} else {
			System.out.println("The rider is not available for mission");
		}
	}

	public void endMission(String name) {
		int index = search(name);
		if (index == -1) {
			System.out.println("The name of a rider does not exist!");
			return;
		}
		System.out.println("the index = " + index);
		riders[index].endMission();
	}

	private int search(String name) {
		int i;
		for (i = 0; i < count; i++) {
			if (name.equals(riders[i].getName()) || name == riders[i].getName()) {
				break;
			}
		}
		if (i != count)
			return i;
		else {
			return -1;
		}
	}

	public String setStatus(String name, int newStatus) {
		int index = search(name);
		if (index == -1) {
			// System.out.println("The name of a rider does not exist!");
			return "The name of a rider does not exist!";
		}
		System.out.println("the index = " + index);
		if (newStatus == riders[index].getStatus()) {
			// System.out.println("new status equals to the old status,nothing to do");
			return "new status equals to the old status,nothing to do";
		}

		if (riders[index].getStatus() == 1 || riders[index].getStatus() == 2) {
			if (newStatus == 0) {
				// System.out.println("the new status and old status does not match");
				return "new status equals to the old status,nothing to do";
			}
		} else if (riders[index].getStatus() == 0) {
			if (newStatus != 1) {
				// System.out.println("the new status and old status does not match");
				return "new status equals to the old status,nothing to do";
			} else {
				riders[index].setStatus(1);
				return "set new status successfully";
			}
		}

		if (newStatus == 3 || riders[index].getStatus() == 3) {
			riders[index].setStatus(newStatus);
			return "set new status successfully";
		}

		riders[index].setStatus(newStatus);
		return "set new status successfully";
	}

	public void updateHours() {
		for (int i = 0; i < count; i++) {
			riders[i].updateHours();
		}

	}

}
